package com.sakura.oj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sakura.oj.constant.CommonConstant;
import com.sakura.oj.mapper.TagMapper;
import com.sakura.oj.model.dto.tag.TagQueryRequest;
import com.sakura.oj.model.entity.Tag;
import com.sakura.oj.service.TagService;
import com.sakura.oj.utils.SqlUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 标签服务实现
 *
 * @author Sakura
 */
@Service
@Slf4j
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {


    /**
     * 获取查询条件
     *
     * @param tagQueryRequest 查询条件
     * @return 查询条件
     */
    @Override
    public QueryWrapper<Tag> getQueryWrapper(TagQueryRequest tagQueryRequest) {
        QueryWrapper<Tag> queryWrapper = new QueryWrapper<>();
        if (tagQueryRequest == null) {
            return queryWrapper;
        }
        Long id = tagQueryRequest.getId();
        String name = tagQueryRequest.getName();
        List<Integer> categoryList = tagQueryRequest.getCategoryList();
        String sortField = tagQueryRequest.getSortField();
        String sortOrder = tagQueryRequest.getSortOrder();

        // 模糊查询
        queryWrapper.like(StringUtils.isNotBlank(name), "name", name);

        // 精准查询
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.in(ObjectUtils.isNotEmpty(categoryList), "category", categoryList);
        // 排序规则
        queryWrapper.orderBy(SqlUtils.validSortField(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }


}
